package com.taoyibao.hongcanting.ui.mine.activity;

import android.view.View;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.activity.BaseActivityPresenter;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.constant.NetCode;
import com.taoyibao.hongcanting.http.DefaultTransformer;
import com.taoyibao.hongcanting.http.JsonApiWrapper;
import com.taoyibao.hongcanting.http.ProgressSubscriber;
import com.taoyibao.hongcanting.model.CompanyBaseModel;
import com.taoyibao.hongcanting.model.UserInfoModel;
import com.taoyibao.hongcanting.ui.mine.delegate.SettingDelegate;

import java.util.List;

/**
 * @author TangAnna
 * @description: 设置
 * @date :${DATA} 11: 55
 */
public class SettingActivity extends BaseActivityPresenter<SettingDelegate> implements View.OnClickListener {
    @Override
    protected Class<SettingDelegate> getDelegateClass() {
        return SettingDelegate.class;
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        viewDelegate.get(R.id.tv_setting_exist).setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getUserInfo();
    }

    /**
     * 获取用户信息
     */
    public void getUserInfo() {
        JsonApiWrapper.getApiService().userInfo(SpUtils.getToken(), SpUtils.getMemberId())
                .compose(new DefaultTransformer<CompanyBaseModel<List<UserInfoModel>>>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel<List<UserInfoModel>>>(this, false) {
                    @Override
                    public void onNext(CompanyBaseModel<List<UserInfoModel>> userInfoModel) {
                        if (userInfoModel.code == NetCode.HTTP_200 && userInfoModel.data != null && userInfoModel.data.size() > 0) {
                            viewDelegate.setData(userInfoModel);
                        } else {
                            super.onNext(userInfoModel);
                        }

                    }
                });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_setting_exist://退出当前账号
                logout();
                break;
        }
    }


    /**
     * 退出
     */
    public void logout() {
        JsonApiWrapper.getApiService().logout(SpUtils.getToken())
                .compose(new DefaultTransformer<CompanyBaseModel>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel>(this, true) {
                    @Override
                    public void onNext(CompanyBaseModel companyBaseModel) {
                        next();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        next();
                    }
                });
    }

    public void next() {
        finish();
        SpUtils.exist();
    }

}
